package rmhc.gui
{
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import rmhc.RMHCApp;
	import rmhc.api.ApiWrapper;
	import rmhc.api.dataflow.PotatoeInfo;
	import rmhc.api.dataflow.UserInfo;
	import rmhc.design.Screen3Design;
	
	public class PostScreen extends Screen3Design
	{
		public var lane:StatusLane;
		public var disp:PotatoeDisplay;
		public var frnd:FriendGrid;
		
		protected var selectedPost:PotatoeInfo;
		protected var postId:String;
		
		public function PostScreen()
		{
			super();
			//tfYdivi.visible = false;
			tfViberi.visible = false;
			status.visible = false;
			lane = new StatusLane();
			lane.x = status.x;
			lane.y = status.y;
			addChildAt( lane , getChildIndex( status ) );
			addEventListener( Event.ADDED_TO_STAGE , init );
			btnPubl.visible = false;
			btnPubl.addEventListener(MouseEvent.CLICK,onPubl );
		}
		
		protected function init( e:Event ):void
		{
			disp = new PotatoeDisplay( IMG_RECT.width , IMG_RECT.height );
			disp.x = IMG_RECT.x;
			disp.y = IMG_RECT.y;
			addChildAt( disp , getChildIndex( btnPubl ) );
			
			lane.init();
			lane.addEventListener( Event.CHANGE , onLaneSelect );
		}
		protected function onLaneSelect( e:Event ):void
		{
			selectedPost = lane.getActive();
			disp.setPotatoe( RMHCApp.getServerUrl() + selectedPost.pic );
			btnPubl.visible = true;
		}
		
		protected function onPubl( e:MouseEvent ):void
		{
			btnPubl.visible = false;
			tfYdivi.visible = false;
			//lane.visible = false;
			var ap:ApiWrapper = RMHCApp.getApi();
			
			ap.preparePostToWall().run();
			//отправялем на сервер, затем показываем выбор друга
			var v:URLVariables = new URLVariables();
			var me:UserInfo = ap.getMe();
			v.senderId = me.uid;
			v.senderFirst = me.firstname;
			v.senderLast = me.lastname;
			v.postType = selectedPost.id;
			v.socialNet = ap.getNetworkId();
			
			var req:URLRequest = new URLRequest( RMHCApp.getServerUrl() + "potatoeSend.php" );
			req.data = v;
			
			var uv:URLLoader = new URLLoader();
			uv.dataFormat = URLLoaderDataFormat.TEXT;
			uv.addEventListener(Event.COMPLETE , onPostSaved );
			uv.load( req );
			
			//скрываем отображение
			disp.mouseEnabled = false;
			TweenLite.to( disp , 0.8 , { alpha : 0 } );
			
			lane.mouseEnabled = lane.mouseChildren = false;
			TweenLite.to( lane , 0.8 , { alpha : 0 } );
		}
		
		protected function onPostSaved( e:Event ):void
		{
			var uv:URLLoader = e.target as URLLoader;
			postId = uv.data;
			if ( postId == "0" )
			{
				return;
			}
			trace( "post saved" , postId );
			
			frnd = new FriendGrid( postId , lane.getThumb() , lane.getThumbSrc() );
			frnd.x = IMG_RECT.x;
			frnd.y = IMG_RECT.y;
			frnd.alpha = 0;
			addChild( frnd );
			frnd.addEventListener(Event.CONNECT , onFrndInit );
			frnd.mouseEnabled = frnd.mouseChildren = false;
			frnd.init();
		}
		
		protected function onFrndInit( e:Event ):void
		{
			tfViberi.alpha = 0;
			tfViberi.visible = true;
			TweenLite.to( tfViberi , 0.5 , { alpha : 1 } );
			
			TweenLite.to( frnd , 0.5 , { alpha : 1 } );
			frnd.mouseEnabled = frnd.mouseChildren = true;
		}
	}
}